package ddu.com.controller.user;

import com.alibaba.fastjson.JSON;
import com.ddu.common.util.date.DateUtil;
import com.ddu.common.util.result.Result;
import ddu.com.aop.AccessToken;
import ddu.com.facade.entity.User;
import ddu.com.facade.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author:zhangxia
 * @createTime: 2018/4/27 15:35
 * @version:1.0
 * @desc:用户controller
 */
@Slf4j
@Controller()
public class UserController {
    @Autowired
    IUserService userService;

    @RequestMapping(value = {"/welcome"}, method = RequestMethod.GET)
    @AccessToken
    public String test(ModelAndView modelAndView, HttpServletRequest request, HttpSession session) {


        int serverPort = request.getServerPort();
        int localPort = request.getLocalPort();
        int remotePort = request.getRemotePort();
        long createTime = session.getCreationTime();
        long lastTime = session.getLastAccessedTime();
        String dd = DateUtil.dateFormatYMD.format(createTime);
        log.info("serverPort={}", serverPort);
        log.info("localPort={}", localPort);  //服务器端口号
        log.info("remotePort={}", remotePort);
        log.info("createTime={}", dd);
        log.info("lastTime={}", lastTime);

        modelAndView.addObject("nihao", "你好");
        log.info("进入啦啦啦啦啦绿");
        return "welcome";
    }


    /**
     * @auth: zhangxia
     * @desc: 注册添加用户
     * @methodName: addUser
     * @params: [user]
     * @return: com.ddu.common.util.result.Result
     * @createTime: 2018/4/27 15:36
     * @version:2.1.6
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Result addUser(User user){
        log.info("入参参数user={}", JSON.toJSONString(user));
        return userService.addUser(user);
    }
}
